//==============================================================================
//
//               Copyright (C) 2002  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2003-12-21 07:22:16 $ $Revision: 1.4 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DArguments.h"
#include "ofc/DText.h"
#include "ofc/DBool.h"
#include "ofc/DInt.h"

#include "DInc.h"
#include "DTest.h"

//-Misc------------------------------------------------------------------------

void DArguments_test()
{
  DArguments *arguments = [DArguments new];
  DText      *date      = [DText new];
  DBool      *quiet     = [DBool new];
  DInt       *pages     = [DInt  new];
  DBool      *verbose   = [DBool new];
  DBool      *speed     = [DBool new];
  char       *argv1[]   = {"./test", "--date=01/12/2003", "-qv"};
  int         argc1     = 3;
  char       *argv2[]   = {"./test", "-d", "15/12/2003", "-p:15"};
  int         argc2     = 4;
  char       *argv3[]   = {"./test", "--dir:/etc", "--verbose", "bike", "moon"};
  int         argc3     = 5;
  DText      *dir       = [DText new];
  DList      *extra     = nil;
  DArgOption  options[] = 
    {
      { "dir=DIR", 'd', "set start direction", dir     },
      { NULL,      's', "set turbo speed",     speed   },
      { "verbose", EOS, "be verbose",          verbose }
    };
  
  STARTTEST();

  [arguments option :"date=DATE"  :'d' :"change the default date" :date   ];
  [arguments option :"quiet"      :'q' :"be very quiet"           :quiet  ];
  [arguments option :"speed"      :'s' :"increase the speed of the program to a higher range, so that the column splitter will do its work" :speed];
  [arguments option :"page=PAGES" :'p' :"set number of pages"     :pages  ];
  [arguments option :NULL         :'v' :"be very verbose"         :verbose];

#if 0
  [arguments printHelp    :"Usage: test [OPTION] ... [FILES]" :"Report bugs to bugs@test.com"];
  [arguments printVersion :"test v1.0\n\nCopyright (C) 2003 by Dick van Oudheusden"];
#endif

  TEST([arguments parse :"test" 
                        :"Usage: test [OPTION] ... [FILES]"
                        :"test v1.0\n\nCopyright (C) 2003 by Dick van Oudheusden"
                        :"Report bugs to bugs@test.com"
                        :argv1
                        :argc1] == nil);

  TEST([quiet   get] == YES);
  TEST([verbose get] == YES);
  TEST([date    ccompare :"01/12/2003"] == 0);
  TEST([pages   get] == 0);
  
  [quiet   set :NO];
  [verbose set :NO];
  [date    set :""];

  TEST([arguments parse :"test" 
                        :"Usage: test [OPTION] ... [FILES]"
                        :"test v1.0\n\nCopyright (C) 2003 by Dick van Oudheusden"
                        :"Report bugs to bugs@test.com"
                        :argv2
                        :argc2] == nil);

  TEST([quiet   get] == NO);
  TEST([verbose get] == NO);
  TEST([pages   get] == 15);
  TEST([date    ccompare :"15/12/2003"] == 0);

  [arguments free];
  
  arguments = [DArguments new];
  
  [arguments options :options :sizeof(options)/sizeof(options[0])];
  
  extra = [arguments parse :"test" 
                           :"Usage: test [OPTION] ... [FILES]"
                           :"test v1.0\n\nCopyright (C) 2003 by Dick van Oudheusden"
                           :"Report bugs to bugs@test.com"
                           :argv3
                           :argc3];
  
  TEST(extra != nil);
  TEST([extra length] == 2);
  TEST([dir ccompare :"/etc"] == 0);
  TEST([[extra get :0] ccompare :"bike"] == 0);
  TEST([[extra get :1] ccompare :"moon"] == 0);
  
  TEST([speed get]   == NO);
  TEST([verbose get] == YES);
  
  [arguments free];

  [date    free ];
  [quiet   free];
  [pages   free];
  [verbose free];
  [speed   free];
  [dir     free];
  [extra   free];
  
  STOPTEST();
}

